package rocky.mvcportlet.control;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import openones.corewa.BaseInForm;
import openones.corewa.BaseOutForm;
import openones.corewa.ReqUtil;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.portlet.PortletFileUpload;
import org.apache.log4j.Logger;

/**
 * @author Thach Le
 * @deprecated Using openones.portlet.control.BaseControl
 */
public class BaseControl {
    final public static Logger LOG = Logger.getLogger("BaseControl");
    final static String ADD_METHOD_ULFILE = "addUploadFile";
    
    protected BaseOutForm outForm = new BaseOutForm();

    /**
     * Default initialization which is invoked in the first time loading of portlet.
     * 
     * @param request
     * @param response
     * @throws PortletException
     * @throws IOException
     */
    public BaseOutForm init(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        LOG.debug("init render.START");

        LOG.debug("init render.END");
        return null;
    }

    /**
     * Default init for action submit.
     * Support multipart form.
     * @param request
     * @param reqMap All submitted data are stored in reqMap
     * @param response
     * @return
     * @throws PortletException
     * @throws IOException
     */
    public BaseOutForm init(ActionRequest request, Map<String, Object> reqMap, ActionResponse response) throws PortletException, IOException {
        LOG.debug("init action.START");

        LOG.debug("init action.END");
        return null;
    }

    public static BaseInForm getData(Map<String, Object> reqMap, Class clazz) {
        return ReqUtil.getData(reqMap, clazz);
    }

    /**
     * This method is invoked by method "processAction" of MVCPortlet.
     * @param req
     * @return
     */
    public static Map<String, Object> getMapData(ActionRequest req) {
        Map<String, Object> result = new HashMap<String, Object>();
        LOG.debug("getMapData.START:");
        Object bean = null;

        if (!PortletFileUpload.isMultipartContent(req)) { // Normal form data
            LOG.debug("Normal form.");
            String fieldName;
            for (Enumeration<String> keyEnum = req.getParameterNames(); keyEnum.hasMoreElements();) {
                fieldName = keyEnum.nextElement();
                result.put(fieldName, req.getParameter(fieldName));
            }
            return result;
        } else { // Upload form data
            LOG.debug("Multipart form.");
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            PortletFileUpload portletFU = new PortletFileUpload(fileItemFactory);
            List items;
            try {
                //req.setCharacterEncoding("utf-8");
                items = portletFU.parseRequest(req);
                FileItem fit;
                String fieldName;
                String value;
                for (Iterator it = items.iterator(); it.hasNext();) {
                    fit = (FileItem) it.next();
                    fieldName = fit.getFieldName();

                    if (fit.isFormField()) { // Normal field
                        value = fit.getString();
                        LOG.debug("Set Form field:" + fieldName + "=" + value);
                        result.put(fieldName, value);
                    } else {
                        LOG.debug("Reach attached file.FieldName= " + fieldName + ";Name=" + fit.getName() + ";Size=" + fit.getSize());
                        result.put(fieldName, fit);
                    }
                }
            } catch (FileUploadException e) {
                LOG.error("Parsing the request", e);
            }
//            } catch (UnsupportedEncodingException ex) {
//                // TODO Auto-generated catch block
//                LOG.error("Parsing the request", ex);
//            }

        }
        LOG.debug("getMapData.END");

        return result;
    }

}
